home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 01 (1986-02-15)(Pacific North-West Amigas Club)[h AFL][b corrupt files].zip / Panorama - Disk 01 (1986-02-15)(Pacific North-West Amigas Club)[h AFL][b corrupt files].adf / mand7.c < prev    next >
Text File  |  1989-10-24  |  7KB  |  263 lines

  1. /*****************************************************************************
  2.  *             MAND7.C - Save Dragon (Write Picture)
  3.  *           Mandelbrot Self-Squared Dragon Generator
  4.  *                  For the Commodore Amiga
  5.  *                       Version 2.01
  6.  *
  7.  *           Copyright (C) 1986, =Robert J. Mical=
  8.  *                Placed in the Public Domain
  9.  *
  10.  *  This program may be distributed free of charge as long as the above
  11.  *  notice is retained.  You may extract any or all information contained
  12.  *  in this file for use in your own programs
  13.  *
  14.  ****************************************************************************/
  15.  
  16. #include "mand.h"
  17.  
  18. extern struct Menu MainMenu[MENU_COUNT];
  19. extern struct MenuItem OptionsItems[OPTIONS_COUNT];
  20.  
  21. extern int MathBase, MathTransBase;
  22.  
  23. extern SHORT Color0, Color1, Color2;
  24. extern SHORT UserPalette[29];
  25.  
  26. /*----------------------*/
  27. /* Graphics definitions */
  28.  
  29. extern struct   GfxBase       *GfxBase;
  30. extern struct   IntuitionBase *IntuitionBase;
  31.  
  32. extern struct   RastPort      *rp,*rp2;
  33. extern struct   ViewPort      *vp;
  34.  
  35. extern struct   Window  *w,*w2, *ColorWindow;
  36. extern struct   Screen  *screen;
  37. extern struct   IntuiMessage  *message;
  38.  
  39. extern long last_color;
  40.  
  41. extern BOOL SettingCenter, SettingBoxSize;
  42.  
  43. /*----------------------------------*/
  44. /* Miscellaneous Global Definitions */
  45.  
  46. extern union kludge {
  47.    float f;
  48.    int i;
  49. } start_r,end_r,start_i,end_i;  /* Block bounds for set */
  50. extern int max_x,max_y,max_mem_y;  /* Graphics window size */
  51. extern int max_count,color_inc,color_offset,color_set,color_mode,color_div;
  52. extern int color_inset,func_num;
  53.  
  54. extern int v_starty,max_mem;
  55. extern long v_offset;
  56. extern UWORD *color_table,*v_mand_store;
  57.  
  58. extern int modified, want_read;
  59.  
  60. extern FILE *console,*v_fp,*redir_fp;
  61.  
  62. extern SHORT ZoomCenterX, ZoomCenterY, ZoomBoxSizeX, ZoomBoxSizeY;
  63. extern SHORT ZoomBoxStartX, ZoomBoxStartY;
  64.  
  65. extern struct NewScreen ns;
  66.  
  67. /* === variables defined here ============================================= */
  68.  
  69. UBYTE *FriendlyTools[] =
  70.     {
  71.     "FILETYPE=Graphicraft",
  72.     };
  73.  
  74. UWORD ProjectObjectData[] =
  75.     {
  76.     0x1FF0, 0x0000,
  77.     0x783C, 0x6000,
  78.     0xE00F, 0xF800,
  79.     0xE00F, 0xF800,
  80.     0x783C, 0x6000,
  81.     0x1FF0, 0x0000,
  82.     /* */
  83.     0x0000, 0x0000,
  84.     0x07C0, 0x0000,
  85.     0x1FF0, 0x0000,
  86.     0x1FF0, 0x0000,
  87.     0x07C0, 0x0000,
  88.     0x0000, 0x0000,
  89.     };
  90.  
  91. struct Image ProjectObjectImage = 
  92.     {
  93.     0, 0,
  94.     21, 6, 2,
  95.     &ProjectObjectData[0],
  96.     3, 0,
  97.     NULL,
  98.     };
  99.  
  100.  
  101. struct DiskObject ProjectObject =
  102.     {
  103.     WB_DISKMAGIC,
  104.     WB_DISKVERSION,
  105.         { 
  106.         /* the Gadget structure */
  107.         NULL,
  108.         0, 0,
  109.         25, 12,
  110.         GADGHBOX | GADGIMAGE,
  111.         RELVERIFY | GADGIMMEDIATE,
  112.         BOOLGADGET,
  113.         (APTR)&ProjectObjectImage,
  114.         NULL,
  115.         NULL,
  116.         0, 0, 0, 0,
  117.         },
  118.     WBPROJECT,
  119.     ":Graphicraft",
  120.     &FriendlyTools[0],
  121.     NO_ICON_POSITION, NO_ICON_POSITION,
  122.     NULL,
  123.     NULL,
  124.     PROCESS_STACKSIZE,
  125.     };
  126.  
  127.  
  128. LONG PictModes[3] =
  129.     {
  130.     CAMG,
  131.     4,
  132.     NULL,
  133.     };
  134.  
  135. LONG PictBody[2] =
  136.     {
  137.     BODY,
  138.     40000,
  139.     };
  140.  
  141.  
  142. struct PaintingHeader PaintingHeader =
  143.     {
  144.     FORM,               /* IFFID */
  145.     (40156),            /* file length (not a nice way (too bad, eh?)) */
  146.     ILBM,               
  147.     BMHD,               
  148.     sizeof(struct BitMapHeader),
  149.         {               /* start of the BitMapHeader structure */
  150.         320, 200,
  151.         0, 0,
  152.         5,
  153.         0, 
  154.         0,
  155.         0,
  156.         0,
  157.         10, 11,
  158.         320, 200,       /* end of BitMapHead */
  159.         },
  160.     CMAP,               
  161.     96,                 /* this value is followed by 32 color registers */
  162. };
  163.  
  164.  
  165.  
  166. BOOL SavePicture(filename)
  167. UBYTE *filename;
  168. {
  169.     SHORT i, j, offset;
  170.     ULONG length, actual_length, file;
  171.     UBYTE *p, component, rgbbyte;
  172.     USHORT rgb;
  173.     BOOL written;
  174.  
  175.     written = FALSE;
  176.  
  177.     /* Open the data file */
  178.     file = Open(filename, MODE_NEWFILE);
  179.     if (file == NULL)
  180.         /* Couldn't open as a new file */
  181.         goto EXIT_ERROR;
  182.  
  183.     /***********************************************************************
  184.      * to write the buffer to the disk, we need to write:
  185.      *      "FORM"
  186.      *      file length
  187.      *      "ILBM"
  188.      *      "BMHD"
  189.      *      BMHD length
  190.      *      BitMapHeader structure
  191.      *      "CMAP"
  192.      *      CMAP length
  193.      *      colors
  194.      *      pad byte if needed
  195.      *      "BODY"
  196.      *      BODY length
  197.      *      body data
  198.      **********************************************************************/
  199.  
  200.      /* First, write out the header */
  201.     length = sizeof(struct PaintingHeader);
  202.     actual_length = Write(file, &PaintingHeader, length);
  203.     if (actual_length != length)
  204.         goto WRITE_ERROR;
  205.  
  206.     /* also, enough bytes for each color register */
  207.     for (i = 0; i < (1 << ns.Depth); i++)
  208.         {
  209.         for (component = 0; component < 3; component++)
  210.             {
  211.             rgb = (GetRGB4(vp->ColorMap, i));
  212.             rgbbyte = (rgb >> (4 * (2 - component)));
  213.             rgbbyte <<= 4;
  214.             actual_length = Write(file, &rgbbyte, 1);
  215.             if (actual_length != 1)
  216.                 goto WRITE_ERROR;
  217.             }
  218.         }
  219.     actual_length = Write(file,  &PictBody[0], 8);
  220.     if (actual_length != 8)
  221.         goto WRITE_ERROR;
  222.  
  223.     /* Finally, the data (uncompressed for now). */
  224.     /* First, get the bytelength per line */
  225.     length = (((ns.Width + 15) >> 4) << 1);
  226.  
  227.     /* For every line ... */
  228.     for (j = 0; j < ns.Height; j++)
  229.         {
  230.         offset = length * j;
  231.         /* For every bit plane ... */
  232.         for (i = 0; i < ns.Depth; i++)
  233.             {
  234.             /* p is set to the start of the line in this bit plane */
  235.             p = (screen->BitMap.Planes[i]) + offset;
  236.             actual_length = Write(file, p, length);
  237.             if (actual_length != length) goto WRITE_ERROR;
  238.             }
  239.         }
  240.  
  241.     PictModes[2] = vp->Modes;
  242.     actual_length = Write(file,  &PictModes[0], 12);
  243.     if (actual_length != 12)
  244.         goto WRITE_ERROR;
  245.     if (PutDiskObject(filename, &ProjectObject) == FALSE)
  246.         goto WRITE_ERROR;
  247.  
  248.     written = TRUE;
  249.  
  250. WRITE_ERROR:
  251.     Close(file);
  252.  
  253. EXIT_ERROR:
  254.     if (NOT written)
  255.         {
  256.         fprintf(console, "*** COULDN'T WRITE YOUR FILE! ***\n");
  257.         DisplayBeep(NULL);
  258.         DeleteFile(filename);
  259.         }
  260.  
  261.     return(written);
  262. }
  263.